<?php


namespace App\Services;

use App\Models\Humidity;

class HumidityService
{
    /**
     * 获取列表
     */
    public function getList($request, $perPage)
    {
        $thisQuery = Humidity::query()->with('archive:id,title');

        if ($request->filled('keyword')) {
            $keyword = $request->input('keyword');
            $thisQuery->where('id', '=', $keyword);
        }
        $list = $thisQuery->paginate($perPage);

        return $list->isNotEmpty() ? $list->toArray() : [];
    }

    /**
     * 新增数据
     */
    public function addNewData($params)
    {
        $humidity = new Humidity();

        $humidity->archive_id  = $params['archive_id'];
        $humidity->temperature = $params['temperature'];
        $humidity->humidity    = $params['humidity'];
        $humidity->warning     = $params['warning'];
        $humidity->creator_id  = user_id();

        $result = $humidity->save();

        CommonService::addActionLog('新增温湿度记录', "数据ID:{$humidity->id}");

        return $result;
    }

    /**
     * 获取详情
     */
    public function detail($id)
    {
        return Humidity::query()->where('id', $id)->first();
    }

    /**
     * 更新
     */
    public function updateData($params, $id)
    {
        $humidity = Humidity::query()->find($id);

        $humidity->archive_id  = $params['archive_id'];
        $humidity->temperature = $params['temperature'];
        $humidity->humidity    = $params['humidity'];
        $humidity->warning     = $params['warning'];

        CommonService::addActionLog('新增温湿度记录', "数据ID:{$id}");

        return $humidity->save();
    }

}
